from zope import component
from zope.event import notify
from zope.lifecycleevent import ObjectModifiedEvent
from zope.publisher.browser import BrowserPage, BrowserView
from zope.app.pagetemplate import ViewPageTemplateFile
from datetime import datetime
from interfaces import IThumbnailGenerator
from cStringIO import StringIO
from miniatura import Generator

class HoraCerta(BrowserPage):

    def __call__(self):
        self.request.response.setHeader('Content-type', 'text/html')
        return '<h1>%s</h1>' %datetime.now().strftime('%H:%M:%S')   
        
class ContatoIndexView2(BrowserPage):

    __call__ = ViewPageTemplateFile('info.pt')
    
    def dia(self):
        return self.context.data.strftime('%d/%m/%Y')
        
class ContatoIndexView(BrowserView):

    def dia(self):
        return self.context.data.strftime('%d/%m/%Y')
        

class ThumbnailView(BrowserPage):
    def __call__(self):
        generator = component.getUtility(IThumbnailGenerator)
        self.request.response.setHeader('content-type','image/jpeg')
        f = StringIO(self.context.data)
        return generator.thumbnail(f)
        
class SetDescription(BrowserPage):
    """A view class called by a form"""

    def update(self, descricao):
        self.context.descricao = descricao
        notify(ObjectModifiedEvent(self.context))
        self.request.response.redirect("index.html")        
